<?php

namespace App\Admin\Http\Controllers\Business;

use App\Admin\Eloquent\ProjectTypeTemplate;
use App\Admin\Eloquent\ProjectTypeTemplateAttribute;
use App\Admin\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Laracasts\Flash\Flash;

/**
 * 项目类型控制器
 *
 * Class project_type_template
 * @package App\Admin\Http\Controllers\Business
 */
class ProjectTypeTemplateController extends Controller
{
    public function index(Request $request)
    {
        $query = $request->only('name');

        $project_type_templates = ProjectTypeTemplate::paging_query($query);

        if (empty($query))
            $project_type_templates->appends($query);

        $links = $project_type_templates->render();

        return view('admin.business.project_type_template.index', compact('query', 'project_type_templates', 'links'));
    }

    /**
     * 添加项目类型表单
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('admin.business.project_type_template.create');
    }

    /**
     * 创建一个项目类型
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request)
    {
        $args = $request->input();

        $project_type_template = new ProjectTypeTemplate($args);

        $project_type_template->save();

        Flash::success("保存一个项目类型成功!");

        return $this->redirectAction("Business\ProjectTypeTemplateController@index");
    }

    /**
     * 修改项目类型模板表单
     *
     * @param int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(int $id)
    {
        $project_type_template = ProjectTypeTemplate::findOrFail($id);

        return view('admin.business.project_type_template.edit', compact('project_type_template'));
    }

    /**
     * 根据id修改指定的项目模板
     *
     * @param Request $request
     * @param int $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request, int $id)
    {
        $args = $request->input();

        $project_type_template = ProjectTypeTemplate::findOrFail($id);

        $project_type_template->update($args);

        Flash::success("更新一个项目类型成功!");

        return $this->redirectAction("Business\ProjectTypeTemplateController@index");
    }

    public function destroy(int $id)
    {
        //TODO 删除项目类型,判断该项目类型是否被使用
        try {
            $project_type_template = ProjectTypeTemplate::findOrFail($id);


            ProjectTypeTemplateAttribute::where('project_type_template_id', $id)->delete();

            $project_type_template->delete();

            Flash::success("删除项目类型成功!");

            return $this->redirectAction("Business\ProjectTypeTemplateController@index");
        } catch (\Exception $e) {
            Flash::error("删除项目类型失败!");

            return $this->redirectAction("Business\ProjectTypeTemplateController@index");
        }

    }
}